﻿using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using Eneta.Portal.Common.Dto;
using Eneta.Portal.Common.Repository;


namespace Eneta.Portal.Profiles
{
    public class LogInControl : UserControl
    {
        public static string ShowLogIn(HttpContext context)
        {
            var user = SPContext.Current.Web.CurrentUser;
            UserProfileDto profile = null;
            string controlName;

            if(user != null)
            {
                var repository = new UserProfileRepository(SPContext.Current.Web);
                profile = repository.GetUserProfileByUserName(user.LoginName);
            }

            if (user != null && profile != null)
                controlName = "AuthenticatedLogInControl.ascx";
            else
                controlName = "AnonymousLogInControl.ascx";
            
            var path = "~/_controltemplates/" + controlName;
            var output = new StringBuilder(10000);

            using(var page = new Page())
            using(var ctl = page.LoadControl(path))
            using(var writer = new StringWriter(output))
            using (var htmlWriter = new HtmlTextWriter(writer))
            {
                ctl.DataBind();
                ctl.RenderControl(htmlWriter);
            }
            var retVal = output.ToString();
            return retVal;
        }

        protected override void OnPreRender(System.EventArgs e)
        {            
            base.OnPreRender(e);
        }
    }
}
